﻿using System.Configuration;
using System.IO;
using EnetaMvc.Infrastructure;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace EnetaMvc.Azure.Services
{
    public class FileClient : IFileClient
    {
        private readonly CloudStorageAccount _account;
        private readonly CloudBlobClient _blobClient;

        public FileClient()
        {
            var connStr = RoleEnvironment.IsAvailable ?
                            CloudConfigurationManager.GetSetting("EnetaDataConnectionString") :
                            ConfigurationManager.AppSettings["EnetaStorageConnectionString"];
            _account = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connStr);
            _blobClient = _account.CreateCloudBlobClient();
        }

        public void SaveFile(string storeName, string fileName, byte[] file)
        {
            var container = _blobClient.GetContainerReference(storeName);
            container.CreateIfNotExists();

            container.SetPermissions(new BlobContainerPermissions()
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });

            var blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.DeleteIfExists();

            using (var mem = new MemoryStream(file))
            {
                mem.Seek(0, SeekOrigin.Begin);
                blockBlob.UploadFromStream(mem);
            }
        }

        public byte[] GetFile(string storeName, string fileName)
        {
            var container = _blobClient.GetContainerReference(storeName);
            container.CreateIfNotExists();

            var blobRef = container.GetBlockBlobReference(fileName);
            if (!blobRef.Exists())
                return null;

            using (var mem = new MemoryStream())
            using (var stream = blobRef.OpenRead())
            {
                stream.CopyTo(mem);
                return mem.ToArray();
            }
        }

        public void Delete(string storeName, string fileName)
        {
            var containerRef = _blobClient.GetContainerReference(storeName);
            containerRef.CreateIfNotExists();

            var blobRef = containerRef.GetBlockBlobReference(fileName);
            blobRef.DeleteIfExists();
        }

        public bool FileExists(string storeName, string fileName)
        {
            var container = _blobClient.GetContainerReference(storeName);
            if (!container.Exists())
                return false;

            var blobRef = container.GetBlockBlobReference(fileName);
            return blobRef.Exists();
        }

        public string GetFileUri(string storeName, string fileName)
        {
            var container = _blobClient.GetContainerReference(storeName);
            if (!container.Exists())
                return "";

            var blobRef = container.GetBlockBlobReference(fileName);
            if (!blobRef.Exists())
                return "";

            return blobRef.Uri.AbsoluteUri;
        }
    }
}
